package org.catdroid.bomba.client;

import java.util.List;

import org.catdroid.bomba.classes.Player;
import org.catdroid.bomba.classes.Players;

import android.os.AsyncTask;

public class AsyncDownloadPlayers extends AsyncTask<Void, Players, Boolean> {

        private OnGameStartedListener onGameStartedListener;
        private OnListChangedListener onListChanged;
        private List<Player> news;
        
        @Override
        protected void onPreExecute() {
                super.onPreExecute();
        }
        
        @Override
        protected Boolean doInBackground(Void... params) {
                Players players = null;
                Boolean finished=false;
                while (!finished)
                {
                        players=Connect2Server.getAllPlayers();
                        publishProgress(players);
                        try {
                                Thread.sleep(100*60);
                        } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                        finished=players.allReady();
                }
                
                return true;
        }
        
        @Override
        protected void onProgressUpdate(Players... players) {
                super.onProgressUpdate(players);
                if (onListChanged != null)
                {
                        onListChanged.onChange(players[0]);
                }
        }
        
        @Override
        protected void onPostExecute(Boolean result) {
                super.onPostExecute(result);
                if(onGameStartedListener != null)
                {
                        onGameStartedListener.onGameStarted();
                }
                
        }
        
        public OnGameStartedListener getOnGameStartedListener() {
                return onGameStartedListener;
        }

        public void setOnGameStartedListener(OnGameStartedListener onGameStartedListener) {
                this.onGameStartedListener = onGameStartedListener;
        }

        public interface OnGameStartedListener {
                
                public void onGameStarted();
                
        }
        
        public interface OnListChangedListener {
                
                public void onChange(Players players);
                
        }

        public OnListChangedListener getOnListChanged() {
                return onListChanged;
        }

        public void setOnListChanged(OnListChangedListener onListCha) {
                onListChanged = onListCha;
        }
}